package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.CustomerMaintainFeeBiz;
import com.egao.orderB.entity.CustomerMaintainFee;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-07-15 14:12:35
 * @version ：1.0
 */
@Repository
public class CustomerMaintainFeeBizImpl extends BaseDao implements CustomerMaintainFeeBiz {

	@Override
	public JsonResult list(PageResult2<T> page, CustomerMaintainFee customerMaintainFee, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<CustomerMaintainFee> rm = new BeanPropertyRowMapper<CustomerMaintainFee>(CustomerMaintainFee.class);
		StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.province,c.city,c.district,c.town");
		sql.append(",c.miles,c.box_type,c.price,c.driver_price");
		sql.append(",c.commission,c.address,c.pinyin,c.dock");
		sql.append(",c.remark,c.create_time,c.create_user,c.update_time");
		sql.append(",c.update_user");
		sql.append(",sogzn.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_customer_maintain_fee c ");
		sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
		sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
		sql.append(" where c.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and c.organization_id=:tableOrganizationId ");
			map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(customerMaintainFee)){
			if(Utils.isNotEmpty(customerMaintainFee.getCustomerId())){
				sql.append(" and instr(c.customer_id,:customer_id)>0 ");
				map.put("customer_id", customerMaintainFee.getCustomerId()+"");
			}
			if(Utils.isNotEmpty(customerMaintainFee.getSort())&&Utils.isNotEmpty(customerMaintainFee.getOrder())){
					sql.append(" order by c."+customerMaintainFee.getSort()+" "+customerMaintainFee.getOrder());
			} else {
					sql.append(" order by c.create_time desc");
			}
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<CustomerMaintainFee> list(CustomerMaintainFee customerMaintainFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<CustomerMaintainFee> rm = new BeanPropertyRowMapper<CustomerMaintainFee>(CustomerMaintainFee.class);
        StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.province,c.city,c.district,c.town");
		sql.append(",c.miles,c.box_type,c.price,c.driver_price");
		sql.append(",c.commission,c.address,c.pinyin,c.dock");
		sql.append(",c.remark,c.create_time,c.create_user,c.update_time");
		sql.append(",c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_customer_maintain_fee c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(customerMaintainFee)){
			if(Utils.isNotEmpty(customerMaintainFee.getCustomerId())){
				sql.append(" and instr(c.customer_id,:customer_id)>0 ");
				map.put("customer_id", customerMaintainFee.getCustomerId()+"");
			}
            if(Utils.isNotEmpty(customerMaintainFee.getSort())&&Utils.isNotEmpty(customerMaintainFee.getOrder())){
				sql.append(" order by c."+customerMaintainFee.getSort()+" "+customerMaintainFee.getOrder());
			} else {
				sql.append(" order by c.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public CustomerMaintainFee getById(CustomerMaintainFee customerMaintainFee) {
		RowMapper<CustomerMaintainFee> rm = new BeanPropertyRowMapper<CustomerMaintainFee>(CustomerMaintainFee.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.customer_id");
        sql.append(",c.province,c.city,c.district,c.town");
		sql.append(",c.miles,c.box_type,c.price,c.driver_price");
		sql.append(",c.commission,c.address,c.pinyin,c.dock");
		sql.append(",c.remark,c.create_time,c.create_user,c.update_time");
		sql.append(",c.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_customer_maintain_fee c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
        sql.append(" where c.deleted=0 and c.id=:id ");
		map.put("id", customerMaintainFee.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(CustomerMaintainFee customerMaintainFee, User loginUser) {
		if (Utils.isNotEmpty(customerMaintainFee.getProvince())) {
			customerMaintainFee.setPinyin(PinyinUtils.ToFirstChar(customerMaintainFee.getProvince()+customerMaintainFee.getCity()
							+customerMaintainFee.getDistrict()+customerMaintainFee.getTown()+customerMaintainFee.getAddress()));
		} else {
			customerMaintainFee.setPinyin(PinyinUtils.ToFirstChar(customerMaintainFee.getAddress()));
		}

		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_customer_maintain_fee (");
        sql.append("organization_id,customer_id,province,city");
		sql.append(",district,town,miles,box_type");
		sql.append(",price,driver_price,commission,address");
		sql.append(",pinyin,dock,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:province,:city");
		sql.append(",:district,:town,:miles,:box_type");
		sql.append(",:price,:driver_price,:commission,:address");
		sql.append(",:pinyin,:dock,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", customerMaintainFee.getCustomerId()==null?null:customerMaintainFee.getCustomerId()+"");
        map.put("province", customerMaintainFee.getProvince());
        map.put("city", customerMaintainFee.getCity());
        map.put("district", customerMaintainFee.getDistrict());
        map.put("town", customerMaintainFee.getTown());
        map.put("miles", customerMaintainFee.getMiles());
        map.put("box_type", customerMaintainFee.getBoxType());
        map.put("price", customerMaintainFee.getPrice());
        map.put("driver_price", customerMaintainFee.getDriverPrice());
        map.put("commission", customerMaintainFee.getCommission());
        map.put("address", customerMaintainFee.getAddress());
        map.put("pinyin", customerMaintainFee.getPinyin());
        map.put("dock", customerMaintainFee.getDock());
        map.put("remark", customerMaintainFee.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(CustomerMaintainFee customerMaintainFee, User loginUser) {
		customerMaintainFee.setPinyin(PinyinUtils.ToFirstChar(customerMaintainFee.getProvince()+customerMaintainFee.getCity()
				+customerMaintainFee.getDistrict()+customerMaintainFee.getTown()+customerMaintainFee.getAddress()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_customer_maintain_fee set ");
        sql.append("province=:province,city=:city ");
		sql.append(",district=:district,town=:town,miles=:miles ");
		sql.append(",box_type=:box_type,price=:price,driver_price=:driver_price ");
		sql.append(",commission=:commission,address=:address,pinyin=:pinyin ");
		sql.append(",dock=:dock,remark=:remark,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("province", customerMaintainFee.getProvince());
        map.put("city", customerMaintainFee.getCity());
        map.put("district", customerMaintainFee.getDistrict());
        map.put("town", customerMaintainFee.getTown());
        map.put("miles", customerMaintainFee.getMiles());
        map.put("box_type", customerMaintainFee.getBoxType());
        map.put("price", customerMaintainFee.getPrice());
        map.put("driver_price", customerMaintainFee.getDriverPrice());
        map.put("commission", customerMaintainFee.getCommission());
        map.put("address", customerMaintainFee.getAddress());
        map.put("pinyin", customerMaintainFee.getPinyin());
        map.put("dock", customerMaintainFee.getDock());
        map.put("remark", customerMaintainFee.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customerMaintainFee.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(CustomerMaintainFee customerMaintainFee, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(customerMaintainFee.getId().length()>0){
			for(String ID : customerMaintainFee.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_customer_maintain_fee set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
